Dino Geek, try to help you

How to use `.htaccess` for custom permalinks on a static site?


Absolutely! Here is a comprehensive explanation of how to use the `.htaccess` file for creating custom permalinks on a static site.

  1. Using `.htaccess` for Custom Permalinks on a Static Site

The `.htaccess` file is a powerful configuration file used by Apache web servers to manage server configurations. One of its common uses is to create custom permalinks, which improve the URL structure, making them more readable and SEO-friendly.

  1. Basic Structure
    A typical `.htaccess` file starts with this basic structure:
    ```

    RewriteEngine On

    ```
    Here, `` ensures that the rewrite module is enabled, and `RewriteEngine On` activates the rewrite engine.

  1. Example 1: Redirecting `example.com/blog` to `example.com/index.html`
    If you want to redirect clean URLs like `example.com/blog` to point to `example.com/index.html`, which is a typical need for static sites, include the following rule:

```

RewriteEngine On

RewriteRule ^blog$ index.html [L]

```
In this case, the `^blog$` regular expression matches the `blog` string. If this condition is met, the user is served the `index.html` file. The `[L]` flag indicates that this is the last rule and should not process any further.

  1. Example 2: Category and Post Permalinks
    To create more complex permalink structures, such as `example.com/category/post-name`, add nested rules.

```

RewriteEngine On

  1. Redirect category pages
    RewriteRule ^category/([a-zA-Z0-9_-]+)/?$ category.html?category=$1 [L]
  1. Redirect post pages
    RewriteRule ^category/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)?$ post.html?category=$1&post=$2 mapping them to `post.html?category=[category-name]&post=[post-name]`.

  1. RewriteBase Directive
    If your site is within a subdirectory, you can use the `RewriteBase` directive to specify your base URL:

```

RewriteEngine On
RewriteBase /subdir/

RewriteRule ^blog$ index.html [L]

  1. Additional rules as needed

    ```
    The `RewriteBase /subdir/` ensures that all rewrite rules will apply to the URLs starting from the `subdir` directory.

  1. Security Considerations
    Using the `.htaccess` file for URL rewriting might expose your site’s internal structure. Be sure to limit access to sensitive files:

```
Order Allow,Deny Deny from all

```
This will restrict access to the `.htaccess` file itself.

  1. Resources
    - Apache HTTP Server Documentation: Provides comprehensive documentation on using the `.htaccess` file and URL rewriting using mod\_rewrite. (Source: https://httpd.apache.org/docs/)
    - The Ultimate Guide to .htaccess Files: A detailed guide from CSS-Tricks on various applications of the `.htaccess` file. (Source: https://css-tricks.com/snippets/htaccess/)
    - \*\*mod_rewrite Introduction\*\*: From the official Apache documentation, this primer explains the fundamental concepts involved with mod_rewrite. (Source: https://httpd.apache.org/docs/current/rewrite/intro.html)

By following these guides and using the provided examples, you can configure custom permalinks effectively, enhancing both usability and SEO of your static site.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use